/**
 * 招聘列表
 * @author  Jello
 * @create  2018/10/22 17:05
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var identity = ""; //用户唯一标识，判断上级依据
var spinner = new Spinner(opts);
var page = 1; //默认页数
var size = 20;
var orderType = ""; //综合排序0工资 1报名人数 2企业评分 3工价 4可借支
var mechanismType = ""; //行业
var workType = ""; //工种1小时工 0正式工
var city = ""; //城市 china全国
var userid_sign = "";

$(function() {
	userid_sign = sessionStorage.getItem("userid_sign");
	identity = sessionStorage.getItem("upIdentity");

	//截取城市选择页面带过来的参数
	var area = getParam('area');
	if(area == null || area == "") {
		$("#city_btn").html("全国");
	} else {
		//decodeURI解码
		var searchCity = decodeURI(area);
		$("#city_btn").html(searchCity);
	}

	var cityValue = $("#city_btn").html();
	//如果从城市选择页面带过来的参数是全国,那么他的值就是china，在根据这个查询所有的招聘信息
	if(cityValue == "全国") {
		city = "china";
	} else {
		city = cityValue;
	}

	//轮播图
	var mySwiper = new Swiper('.swiper-container', {
		initialSlide: 0,
		// 轮播图的方向，也可以是vertical方向
		direction: 'horizontal',
		//播放速度
		loop: true,
		observer: true,
		observeParents: true,
		// 自动播放时间
		autoplay: 4500,
		// 播放的速度
		speed: 300,
		// 如果需要分页器，即下面的小圆点
		pagination: '.swiper-pagination',
		// 这样，即使我们滑动之后， 定时器也不会被清除
		autoplayDisableOnInteraction: false,
		paginationClickable: true,
		pagination: '.swiper-pagination',
		paginationType: 'custom',
		paginationCustomRender: function(swiper, current, total) {
			var _html = '';
			for(var i = 1; i <= total; i++) {
				if(current == i) {
					_html += '<span class="swiper-pagination-customs swiper-pagination-customs-active"></span>';
				} else {
					_html += '<span class="swiper-pagination-customs"></span>';
				}
			}
			return _html; //返回所有的页码html
		}
	});

//	backLogin(userid_sign, "../../login.html", function() {
		//查询所有行业
		queryMechanismList(function(resultObj) {
			$("#item_trade").empty();
			if(resultObj.data.mechanismTypeList != null && resultObj.data.mechanismTypeList.length > 0) {
				for(var i = 0; i < resultObj.data.mechanismTypeList.length; i++) {
					var html = '<div class="item_div" onclick="filterMechanismType(' + resultObj.data.mechanismTypeList[i].id + ')">' + resultObj.data.mechanismTypeList[i].mechanismTypeName + '</div>';
					$("#item_trade").append(html);
				}
			} else {
				$("#trade_null").css("display", "block");
			}
		})

		//查询招聘列表
		var param = {};
		param.identity = identity;
		param.page = page;
		param.mechanismType = mechanismType;
		param.workType = workType;
		param.orderType = orderType;
		param.city = city;
		param.type = 1;
		param.mechanismName = "";
		param.userid_sign = userid_sign;
		queryWorkList(param, function(resultObj) {
			//主页的轮播图
			if(resultObj.data.slideshowList != null && resultObj.data.slideshowList.length > 0) {
				for(var i = 0; i < resultObj.data.slideshowList.length; i++) {
					var hot = '<div class="swiper-slide" onclick="queryWorkDetail(' + resultObj.data.slideshowList[i].id + ')">' +
						'<img src="' + resultObj.data.slideshowList[i].mechanismUrl + '"/></div>';
					$(".swiper-wrapper").append(hot);
					mySwiper.init();
				}
			}
			//有招聘数据
			if(resultObj.data.workList != null && resultObj.data.workList.length > 0) {
				showWorkData(resultObj);
			} else {
				//没有数据就显示无数据图--并且要把排序，筛选这块隐藏
				$("#condition_btn").hide();
				$("#query_not_result").show();
			}
		});
//	})

	//用于加载的时候
	$(window).scroll(function() {
		var scrollTop = $(window).scrollTop(); //这个方法是当前滚动条滚动的距离
		var scrollHeight = $(window).height(); //获取当前窗体的高度   可見高度
		var windowHeight = $(document).height(); // 是获取整个页面的高度
		if(scrollTop >= windowHeight - scrollHeight) {
			flipOver();
		}
	});

	//筛选显示
	$("#filter_btn").click(function() {
		if($("#order_content").show()) {
			$("#order_content").hide();
		}
		$(".saixuan").css("display", "block");
		$(".saix_box").css("display", "block");
		$(".saixuan_box").css("display", "block");

		//固定
		$('body').css({
			position: 'fixed',
			left: '0px',
			right: '0px',
			top: '0px',
			bottom: '0px'
		})
		window.ontouchmove = function(e) {
			e.preventDefault && e.preventDefault();
			e.returnValue = false;
			e.stopPropagation && e.stopPropagation();
			return false;
		};
	})

	//排序显示
	$("#order_btn").click(function() {
		$(".list_recru").toggle();
		if($(".list_recru").is(":hidden")) {
			$('body').css({
				position: 'static'
			})
			window.ontouchmove = "";
		} else {
			$('body').css({
				position: 'fixed',
				left: '0px',
				right: '0px',
				top: '0px',
				bottom: '0px'
			})
			window.ontouchmove = function(e) {
				e.preventDefault && e.preventDefault();
				e.returnValue = false;
				e.stopPropagation && e.stopPropagation();
				return false;
			};
		}
	})

	//筛选遮罩层关闭
	$(".saixuan_box").click(function() {
		$('body').css({
			position: 'static'
		})
		window.ontouchmove = "";
		$(".saix_box").css("display", "none");
		$(".saixuan_box").css("display", "none");
		$(".saixuan").css("display", "none");
	})

	//排序遮罩层关闭
	$(".recru_back_box").click(function() {
		$('body').css({
			position: 'static'
		})
		window.ontouchmove = "";
		$(".list_recru").hide();
	})

	//行业选中样式效果
	$("#item_trade").on("click", ".item_div", function() {
		$(this).addClass("itemaddclass").siblings().removeClass("itemaddclass");
	})

	//工种选中样式效果及传递参数
	$("#item_prodcution").on("click", ".item_div", function() {
		$(this).addClass("itemaddclass").siblings().removeClass("itemaddclass");
		var prod_text = $(this).text();
		if(prod_text == "正式工") {
			workType = 0;
		} else if(prod_text == "小时工") {
			workType = 1;
		}
	})

	//筛选重置
	$("#reset_btn").click(function() {
		workType = "";
		mechanismType = "";
		$("#item_trade .item_div").removeClass("itemaddclass");
		$("#item_prodcution .item_div").removeClass("itemaddclass");
		$(".c_px2").css({
			"color": "",
			"font-weight": ""
		});
		$(".c_ovx2").attr("src", "../../img/work/rec002_09.png");
	})

	//筛选确定
	$("#filter_surebtn").click(function() {
		if(workType.toString() != "" || mechanismType.toString() != "") {
			//筛选样式高亮
			$(".c_px2").css({
				"color": "#3CAFFF",
				"font-weight": "bold"
			});
			$(".c_ovx2").attr("src", "../../img/work/saixg2x.png");
		}
		//滚屏效果
		$('body').css({
			position: 'static'
		})
		window.ontouchmove = "";
		//查询招聘列表
		var param = {};
		param.identity = identity;
		param.page = 1;
		param.mechanismType = mechanismType;
		param.workType = workType;
		param.orderType = orderType;
		param.city = city;
		param.type = 1;
		param.mechanismName = "";
		param.userid_sign = userid_sign;
		queryWorkList(param, function(resultObj) {
			$(".saixuan").css("display", "none");
			$('#worklist').empty();
			//有招聘数据
			if(resultObj.data.workList != null && resultObj.data.workList.length > 0) {
				showWorkData(resultObj);
			} else {
				//没有数据就显示无数据图
				$("#query_not_result").show();
			}
		})
	})

	//综合排序选中
	$(".list_recru .recru_back").on("click", ".recru_margin", function() {
		//综合排序点击时效果
		$('body').css({
			position: 'static'
		})
		window.ontouchmove = ""; //清空触摸
		$(this).addClass("recre_fontactive").siblings().removeClass("recre_fontactive");
		$(this).addClass("recru_marginimg").siblings().removeClass("recru_marginimg");
		$(".c_px").css({
			"color": "#3CAFFF",
			"font-weight": "bold"
		});
		$(".c_pxborder").css("display", "block");
		$(".c_ovx").attr("src", "../../img/work/rec002_10.png");
		//选中的排序类型
		orderType = $(this).children("input").val();
		$("#order_content").css("display", "none");

		//查询招聘列表
		var param = {};
		param.identity = identity;
		param.page = 1;
		param.mechanismType = mechanismType;
		param.workType = workType;
		param.orderType = orderType;
		param.city = city;
		param.type = 1;
		param.mechanismName = "";
		param.userid_sign = userid_sign;
		queryWorkList(param, function(resultObj) {
			$(".saixuan").css("display", "none");
			$('#worklist').empty();
			//有招聘数据
			if(resultObj.data.workList != null && resultObj.data.workList.length > 0) {
				showWorkData(resultObj);
			} else {
				//没有数据就显示无数据图
				$("#query_not_result").show();
			}
		})
	})

	//跳转搜索页面
	$("#search_input").click(function() {
		window.location.href = "search.html";
	})

	//跳转城市选择页面,通过href带参的城市需要encodeURI进行编码，以避免乱码
	$("#selectArea").click(function() {
		var area = $("#city_btn").html();
		window.location.href = encodeURI("city_list.html?area=" + area);
	});

})

/**
 * 选中行业传递赋值
 */
function filterMechanismType(id) {
	mechanismType = id;
}

/**
 * 翻页-下拉加载
 */
function flipOver() {
	page++;
	//查询招聘列表
	var param = {};
	param.identity = identity;
	param.page = page;
	param.mechanismType = mechanismType;
	param.workType = workType;
	param.orderType = orderType;
	param.city = city;
	param.type = 1;
	param.mechanismName = "";
	param.userid_sign = userid_sign;
	queryWorkList(param, function(resultObj) {
		//有招聘数据
		if(resultObj.data.workList != null && resultObj.data.workList.length > 0) {
			showWorkData(resultObj);
		}
		//全部数据加载完成
		var length = resultObj.data.workList.length;
		finishPage(length);
	})
}

/**
 * 全部数据加载完成
 * @param {Object} length
 */
function finishPage(length) {
	if(length % size != 0 || length == 0) {
		setTimeout(function() {
			mui.toast('已全部加载');
		}, 300);
	}
}

/**
 * 显示招聘数据
 * @param {Object} resultObj 列表数据
 */
function showWorkData(resultObj) {
	$("#query_not_result").hide();
	var lendType = ""; //借支
	var isShow = "";
	var workType = ""; //工种
	var workType_isShow1 = "";
	var workType_isShow2 = "";
	var isApply = ""; //是否报名
	var recruitStatus1 = ""; //招工状态
	var recruitStatus2 = "";
	var key = ""; //关键字
	for(var i = 0; i < resultObj.data.workList.length; i++) {

		var lend_type = resultObj.data.workList[i].lendType;
		var post_type = resultObj.data.workList[i].postType;
		var reStatus = "none"; //返回显示1

		if(lend_type == 1) {
			isShow = "block";
			lendType = "可借支";
		} else if(lend_type == 0) {
			isShow = "none";
		}
		if(post_type == 1) {
			workType_isShow2 = "none";
			workType_isShow1 = "block";
			workType = "小时工";
		} else if(post_type == 0) {
			workType = "正式工";
			workType_isShow1 = "none";
			workType_isShow2 = "block";
		}
		if(resultObj.data.workList[i].isApply == 0) {
			isApply = "block";
		} else {
			isApply = "none";
		}
		if(resultObj.data.workList[i].status == 1) {
			recruitStatus1 = "block";
			recruitStatus2 = "none";
		} else {
			recruitStatus1 = "none";
			recruitStatus2 = "block";
		}
		if(resultObj.data.workList[i].key == null) {
			key = "";
		} else {
			key = resultObj.data.workList[i].key;
		}
		if(resultObj.data.workList[i].reStatus == 1) {
			reStatus = "block";
		}

		var html = '<div class="mui-table-view-celll"><div class="item_box2" onclick="queryWorkDetail(' + resultObj.data.workList[i].id + ')">' +
			'<div class="item_box">' +
			'<div class="box_f">' + resultObj.data.workList[i].mechanismName + '</div>' +
			'<div class="f_jiezi" id="f_jiezi"' + "style=display:" + isShow + "" + '>' + lendType + '</div>' +
			'<div class="box_fs"><span>' + resultObj.data.workList[i].mechanismScore + '</span>分</div>' +
			'<div class="box_b">' +
			'<img class="b_img" style="width: 1.893333rem;height: 1.486666rem;" src="' + resultObj.data.workList[i].mechanismUrl + '" />' +
			'<div class="yibaoming" ' + "style=display:" + isApply + "" + '>已报名</div>' +
			'<div class="b_moneythree">' +
			'<div class="b_money" ' + "style=display:" + workType_isShow2 + "" + '>' + resultObj.data.workList[i].wageRange + '元/月</div>' +
			'<div class="b_money" ' + "style=display:" + workType_isShow1 + "" + '>' + resultObj.data.workList[i].workMoney + '元/小时</div>' +
			'<div class="b_three bthree_key">' +
			'<div class="item_b_box">' + key + '</div>' +
			'</div>' +
			'<div class="b_fore_box">' +
			'<div class="x_width" ' + "style=display:" + recruitStatus1 + "" + '>已招满</div>' +
			'<div class="x_width" ' + "style=display:" + recruitStatus2 + "" + '>需' + resultObj.data.workList[i].workTypeName + '' + resultObj.data.workList[i].maxNumber + '人</div>' +
			'<div style="margin-right:5%;float:right">已报名' + resultObj.data.workList[i].applyNumber + '人</div>' +
			'</div>' +
			'</div>' +
			'<div class="b_renyuan">' +
			'<div class="b_money"><span>' + workType + '</span></div>' +
			'</div>' +
			'</div>' +
			'<div class="re_fire" ' + "style=display:" + reStatus + "" + '><span class="re_money">' + resultObj.data.workList[i].reMoney + '</span></div>' +
			'</div>' +
			'</div></div>';

		$('#worklist').append(html);

	}
}

/**
 * 招聘详情页面
 * @param {Object} id 招聘id
 */
function queryWorkDetail(id) {
	window.location.href = "recruitmentlist_detailH5.html?id=" + id;
}